﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class YP_LJTZD : Form
    {
        int insert_switch;
        string cmd_ZSG = "BEGIN TRANSACTION ";
        SqlDataAdapter da = new SqlDataAdapter();//实例化sqldataadpte


        public YP_LJTZD()
        {
            InitializeComponent();
        }
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if ((ActiveControl is TextBox || ActiveControl is Button) && keyData == Keys.Enter)
                keyData = Keys.Tab;
            return base.ProcessDialogKey(keyData);
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            slidePanel1.IsOpen = false;
        }

        private void YP_LJTZD_Load(object sender, EventArgs e)
        {
            slidePanel1.IsOpen = false;
            dateTimeInput1.Value = DateTime.Today;
            dateTimeInput2.Value = DateTime.Today;
            txt_DH.Text = DateTime.Today.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2,'0')+ DateTime.Now.Day.ToString().PadLeft(2,'0') + DateTime.Now.Hour.ToString().PadLeft(2,'0') + DateTime.Now.Minute.ToString().PadLeft(2,'0') + DateTime.Now.Second.ToString().PadLeft(2,'0');

            try
            {
                using (SqlConnection con = new SqlConnection(SQL.connectDB))
                {
                    DataTable dt = new DataTable();
                    SqlCommand cmd1 = new SqlCommand("SELECT BM,DH,MC,DJ FROM ZD_YP", con);//sql语句
                    da.SelectCommand = cmd1;//设置为已实例化SqlDataAdapter的查询命令
                    da.Fill(dt);
                    dataGridViewX2.DataSource = dt;//药品字典
                    if (YP_LJTZCZ.currow != null)
                    {
                        txt_DH.Text = YP_LJTZCZ.currow.ToString();
                        insert_switch = 0;
                    }
                    else
                    {
                        txt_DH.Text = DateTime.Today.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0') + DateTime.Now.Hour.ToString().PadLeft(2, '0') + DateTime.Now.Minute.ToString().PadLeft(2, '0') + DateTime.Now.Second.ToString().PadLeft(2, '0');
                        insert_switch = 1;
                        //dt_czd.Columns[0].DefaultValue = txt_DH.Text.ToString();
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        #region 保存
        private void save()
        {
            if (YP_LJTZCZ.state_flag == 2)
                MessageBox.Show("该冲账单号已记账,不能更改!", "提示");
            else if (YP_LJTZCZ.state_flag == 1)
                MessageBox.Show("该冲账单号已审核,请先消审!", "提示");
            else
            {
                cmd_ZSG = cmd_ZSG + " COMMIT TRANSACTION ";

                try
                {
                    SqlConnection cn = new SqlConnection(SQL.connectDB);

                    {
                        cn.Open();

                        SqlCommand com = new SqlCommand(cmd_ZSG, cn);

                        com.ExecuteNonQuery();

                        cmd_ZSG = "BEGIN TRANSACTION ";

                    }
                }
                catch { }
                MessageBox.Show("保存成功", "提示");
            }
        }
        #endregion
        private void buttonX3_Click(object sender, EventArgs e)
        {
            string cxtj="";
            if(txt_BM1.Text !="" && txt_BM2.Text!="")
                cxtj=cxtj+" AND BM>="+txt_BM1.Text.ToString()+" AND BM=<"+txt_BM2.Text.ToString();
            if (cmb_flag.Text != "")
                cxtj = cxtj + " AND FLAG=" + cmb_flag.SelectedIndex;
            if (cmb_JB.Text != "")
                cxtj = cxtj + " AND JB=" + cmb_JB.SelectedIndex + 1;
            if (cmb_JX.Text != "")
                cxtj = cxtj + " AND JX=" + cmb_JB.SelectedIndex + 1;
            try
            {
                using (SqlConnection con = new SqlConnection(SQL.connectDB))
                {
                    DataTable dt_bmdw = new DataTable();
                    SqlCommand BMDW = new SqlCommand("SELECT BM 编码,MC 名称,GG 规格,DJ 原单价,'' 新单价 FROM ZD_YP WHERE 1=1" + cxtj, con);//sql语句
                    da.SelectCommand = BMDW;//设置为已实例化SqlDataAdapter的查询命令
                    da.Fill(dt_bmdw);
                    dataGridViewX1.DataSource = dt_bmdw;
                  
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridViewX1_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                txt_BM.Text = dataGridViewX1.CurrentRow.Cells[0].Value.ToString();
                txt_MC.Text = dataGridViewX1.CurrentRow.Cells[1].Value.ToString();
                txt_GG.Text = dataGridViewX1.CurrentRow.Cells[2].Value.ToString();
                txt_YDJ.Text = dataGridViewX1.CurrentRow.Cells[3].Value.ToString();
                txt_XDJ.Text = dataGridViewX1.CurrentRow.Cells[4].Value.ToString();
            }
            catch { }
           
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
                
            dataGridViewX1.CurrentRow.Cells[4].Value = txt_XDJ.Text.ToString();

            try
            {
                using (SqlConnection con = new SqlConnection(SQL.connectDB))
                {
                    con.Open();
                    SqlCommand sc = new SqlCommand("SELECT COUNT(*) FROM YP_LJTZCZD WHERE TJDH='" + txt_DH.Text.ToString() + "' AND YPBM='" + txt_BM.Text.ToString() + "'", con);
                    da.SelectCommand = sc;
                    if (Convert.ToInt16(sc.ExecuteScalar()) > 0)
                    {
                        cmd_ZSG = cmd_ZSG + " UPDATE YP_LJTZCZD SET XDJ='" + txt_XDJ.Text.ToString() + "'";
                        dataGridViewX1.CurrentRow.Cells[4].Value = txt_XDJ.Text.ToString();

                    }

                    else
                    {
                        cmd_ZSG = cmd_ZSG + " INSERT INTO YP_LJTZCZD(TJDH,YPBM,YPMC,YDJ,XDJ) VALUES('" + txt_DH.Text.ToString() + "','" + txt_BM.Text.ToString() + "','" + txt_MC.Text.ToString() + "','"
                                  + txt_YDJ.Text.ToString() + "','" + txt_XDJ.Text.ToString()+ "')";

                        if (insert_switch == 1)
                        {
                            cmd_ZSG = cmd_ZSG + " INSERT INTO YP_LJTZCZ(TJDH,RQ,LRRY,FLAG,BZ,TJLX,PWMC,PWDW,PWRQ) VALUES ('" + txt_DH.Text.ToString() + "','" + dateTimeInput1.Value + "','陈立辉','0','" + txt_BZ.Text.ToString() + "','" + cmb_TJLX.Text.ToString() + "','" + txt_PWMC.Text.ToString() + "','" + txt_PWDW.Text.ToString() + "','" + dateTimeInput2.Value + "')";

                            insert_switch = 0;
                        }
                    }
                }
            }
            catch { }
          
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            save();
        }

        private void txt_BM1_TextChanged(object sender, EventArgs e)
        {

        }

    }
}